package bb.lanxing.util.pop;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;

public class TaskManager {
    private final PriorityBlockingQueue<Task> queue = new PriorityBlockingQueue<>();
    private final ArrayList<Task> tasksSuccess = new ArrayList<>();
    private final PopManager popManager = new PopManager();

    private static final class InstanceHolder {
        private static final TaskManager INSTANCE = new TaskManager();
    }

    public static TaskManager getInstance() {
        return InstanceHolder.INSTANCE;
    }

    public void pushToPopManager(PopDialog popDialog) {
        this.popManager.pushToQueue(popDialog);
    }

    public void pushToPopManager(List<PopDialog> list) {
        for (PopDialog popDialog : list) {
            this.popManager.pushToQueue(popDialog);
        }
    }

    public TaskManager pushToQueue(Task task) {
        if (!isAlreadyInQueue(task, this.queue)) {
            this.queue.add(task);
        } else {
            Task taskFromQueue = getTaskFromQueue(task.getTaskId());
            if (taskFromQueue != null) {
                onTaskInterrupt(taskFromQueue);
            }
            this.queue.add(task);
        }
        return this;
    }

    public Task getTaskFromQueue(int i) {
        PriorityBlockingQueue<Task> priorityBlockingQueue = this.queue;
        if (priorityBlockingQueue != null) {
            for (Task next : priorityBlockingQueue) {
                if (next.getTaskId() == i) {
                    return next;
                }
            }
        }
        return null;
    }

    private void removeFromQueue(Task task) {
        synchronized (this) {
            this.queue.remove(task);
            Log.d("RM removeFromQueue", "Task = " + task.getTaskId());
        }
    }

    public void runTask() {
        for (Task next : this.queue) {
            try {
                Log.d("RM runTask", "Task = " + next.getTaskId());
            if (next.getTaskStatus() == 0) {
                    next.run();
                    next.setTaskStatus(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void interruptAllTasks() {
        Log.d("RM interruptAllTasks", "queue size " + this.queue.size());
        for (Task next : this.queue) {
            Log.d("RM interruptAllTasks", "queue id " + next.getTaskId());
            try {
                if (next.getTaskWork() != null) {
                    next.getTaskWork().onWorkInterrupt(next);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.tasksSuccess.clear();
        this.queue.clear();
        this.popManager.dismissCurrent();
        this.popManager.clear();
    }

    public boolean isEmpty() {
        return this.queue.isEmpty();
    }

    private boolean isAlreadyInQueue(Task task, PriorityBlockingQueue<Task> priorityBlockingQueue) {
        for (Task value : priorityBlockingQueue) {
            if (value.getTaskId() == task.getTaskId()) {
                return true;
            }
        }
        return false;
    }

    private boolean isTaskFirstPriority(Task task) {
        synchronized (this) {
            Task task2 = null;
            try {
                if (!this.queue.isEmpty()) {
                    task2 = this.queue.element();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (task2 == null || task2.getTaskId() != task.getTaskId()) {
                return false;
            }
            Log.d("RM isTaskFirstPriority", "Task " + task.getTaskId() + "   |    firstTask :" + task2.getTaskId());
            return true;
        }
    }

    public void onTaskGoSuccess(Task task) {
        synchronized (this) {
            task.setTaskStatus(2);
            this.tasksSuccess.add(task);
            Log.d("RM onTaskGoSuccess", "Task " + task.getTaskId() + " status = " + task.getTaskStatus());
            if (isTaskFirstPriority(task)) {
                checkFirstAndNotify();
            } else if (task.getPopdialogs() != null && !task.getPopdialogs().isEmpty()) {
                pushToPopManager(task.getPopdialogs());
            }
        }
    }

    public void onTaskInterrupt(Task task) {
        synchronized (this) {
            task.setTaskStatus(3);
            Log.d("RM onTaskInterrupt", "Task = " + task.getTaskId());
            removeFromQueue(task);
            checkFirstAndNotify();
        }
    }

    public void checkFirstAndNotify() {
        synchronized (this) {
            if (this.queue.isEmpty()) {
                return;
            }
            Task element = this.queue.element();
            Log.d("RM checkFirstAndNotify", "Task " + element.getTaskId() + "  status = " + element.getTaskStatus());
            if (this.tasksSuccess.contains(element)) {
                notifyTaskPopDialog(element);
                removeFromQueue(element);
            }
        }
    }

    private void notifyTaskPopDialog(Task task) {
        if (task.getPopdialogs() == null || task.getPopdialogs().isEmpty()) {
            return;
        }
        pushToPopManager(task.getPopdialogs());
        this.popManager.showNextPopDialog();
    }

    public void dismissCurrent() {
        this.popManager.dismissCurrent();
    }
}
